package com.vote.admin.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.vote.admin.vo.result.ViewResult;
import com.vote.common.constant.RedisCacheTime;
import com.vote.common.constant.RedisConstants;
import com.vote.common.entity.VoteMenu;
import com.vote.common.entity.VoteProject;
import com.vote.common.entity.VoteUser;
import com.vote.common.enums.AdminSessionEnum;
import com.vote.common.enums.UserTypeEnum;
import com.vote.common.returns.RespCode;
import com.vote.common.returns.RespEntity;
import com.vote.common.service.VoteMenuService;
import com.vote.common.service.VoteProjectService;
import com.vote.common.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Service
public class AdminViewService {

    @Autowired
    private VoteProjectService voteProjectService;

    /**
     * 投票活动状态统计
     * @return
     */
    public RespEntity<ViewResult> getViewInfo() {
        //按照活动状态分组聚合
        QueryWrapper<VoteProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("project_status, COUNT(*) AS project_rule");
        queryWrapper.lambda().eq(VoteProject::getDeleted, 0)
                .groupBy(VoteProject::getProjectStatus);
        List<VoteProject> voteProjectList = voteProjectService.list(queryWrapper);

        ViewResult viewResult = new ViewResult();
        if (CollectionUtil.isNotEmpty(voteProjectList)) {
            for (VoteProject voteProject : voteProjectList) {
                switch (voteProject.getProjectStatus()) {
                    case 1 : viewResult.setWait(voteProject.getProjectRule()); break;
                    case 2 : viewResult.setRun(voteProject.getProjectRule()); break;
                    case 3 : viewResult.setFinish(voteProject.getProjectRule());
                }
            }
        }

        return new RespEntity<>(RespCode.SUCCESS, viewResult);
    }
}
